import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Select {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/tmp1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2.创立数据库连接
        Connection connection = dataSource.getConnection();
        //3.创建SQL语句
        String sql = "select * from student";
        //4.把sql语句和数据库连接信息给执行类
        PreparedStatement statement = connection.prepareStatement(sql);
        //5.执行sql语句
       ResultSet resultSet = statement.executeQuery();
       //6.遍历结果集
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(id + " " + name);
        }
        //7.关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
